package CCRMClient.GUI;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;

import javax.swing.JLabel;

import java.awt.Font;
import java.util.ArrayList;

import javax.swing.JButton;

import CCRMClient.ClientStart;
import Common.SERVER_COMMANDS;
import Common.Entities.Customer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.LineBorder;
import javax.swing.SwingConstants;
/**
 * This class is a screen of assign customers to a marketing template
 * @author AL
 *
 */
public class CustomersForSaleScreen extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel lblCustomersForSale;
	private JButton btnAddressCustomer;
	private JButton btnReturnToMy;
	private String saleNum;
	private ArrayList<String[]> customers;
	private String selected = null;
	private JTable tblMyCustomers;
	private JScrollPane scrollPane;

	public CustomersForSaleScreen(ArrayList<String[]> customers) {
		setSize (550, 366);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		this.customers = customers;		
		initComponents();
		loadMyCustomers();
		btnAddressCustomer.setEnabled(true);
	}

	private void initComponents() {
		setBackground(Color.WHITE);
		setLayout(null);
		
		lblCustomersForSale = new JLabel("Customers For Sale");
		lblCustomersForSale.setHorizontalAlignment(SwingConstants.CENTER);
		lblCustomersForSale.setForeground(Color.RED);
		lblCustomersForSale.setFont(new Font("David", Font.BOLD, 26));
		lblCustomersForSale.setBounds(0, 11, 550, 25);
		add(lblCustomersForSale);
		
		btnAddressCustomer = new JButton("Address Customer");
		btnAddressCustomer.addActionListener(new BtnAddressCustomerActionListener());
		btnAddressCustomer.setEnabled(false);
		btnAddressCustomer.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnAddressCustomer.setBounds(296, 276, 144, 23);
		add(btnAddressCustomer);
		
		btnReturnToMy = new JButton("Return to my sales");
		btnReturnToMy.addActionListener(new BtnReturnToMyActionListener());
		btnReturnToMy.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnReturnToMy.setBounds(10, 276, 149, 23);
		add(btnReturnToMy);
		add(getScpMyCustomers());
	}
	private void loadMyCustomers() {
		if (!customers.isEmpty()) {
			DefaultTableModel dtm = (DefaultTableModel) tblMyCustomers.getModel();
			dtm.removeRow(0);
			for (String[] row : customers) {
				dtm.addRow(row);
			}
		}
	}
		JScrollPane getScpMyCustomers() {
			if (scrollPane == null) {
				scrollPane = new JScrollPane();
				scrollPane.setBounds(20, 49, 477, 187);
				scrollPane.setViewportView( getTblMyCustomers());
			}
			return scrollPane;
		}
		JTable getTblMyCustomers() {
				if (tblMyCustomers == null) {
					tblMyCustomers = new JTable();
					tblMyCustomers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					tblMyCustomers.setModel(new DefaultTableModel(
						new Object[][] {
							{null, null, null, null, null},
						},
						new String[] {
							"Sale ID", "Customer ID", "Customer Name", "Customer Phone", "Customer Address"
						}
					) {
						Class[] columnTypes = new Class[] {
							String.class, String.class, String.class, String.class, String.class
						};
						public Class getColumnClass(int columnIndex) {
							return columnTypes[columnIndex];
						}
						boolean[] columnEditables = new boolean[] {
							false, false, false, false, true
						};
						public boolean isCellEditable(int row, int column) {
							return columnEditables[column];
						}
					});
					tblMyCustomers.getColumnModel().getColumn(0).setPreferredWidth(50);
					tblMyCustomers.getColumnModel().getColumn(0).setMaxWidth(51);
					tblMyCustomers.getColumnModel().getColumn(1).setMaxWidth(100);
					tblMyCustomers.setFont(new Font("David", Font.BOLD, 11));
				}
				return tblMyCustomers;
			}
		/**
		 * This inner class implements event listener for pressing Return to my sales button
		 * @author Ori Ziv
		 *
		 */
	private class BtnReturnToMyActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			try {
				ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.VIEW_SALES_FOR_SALESMAN, ClientStart.user.getID());
			} catch (IOException e1) {
				JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
				ClientStart.quitApp();
			}
		}
	}
	/**
	 * This inner class implements event listener for pressing Address Customer button
	 * @author Ori Ziv
	 *
	 */
	private class BtnAddressCustomerActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int selected = tblMyCustomers.getSelectedRow();
			if (selected == -1) {
				JOptionPane.showMessageDialog(null, "Please Select a customer", "Address Customer", JOptionPane.INFORMATION_MESSAGE);
			} else {
				DefaultTableModel dtm = (DefaultTableModel) tblMyCustomers.getModel();
				String[] data = {(String) dtm.getValueAt(selected, 0), (String) dtm.getValueAt(selected, 1)};
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADDRESS_CUSTOMER, data);
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			}
		}
	}
}
